<template>
  <div class="list">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      class="list-container"
    >
    <div
      v-for="item in list"
      :key="item.id"
      is="router-link"
      :to="{name: 'detail', params: {id: item.id}}"
      tag="div"
      class="list-item"
    >
      <img :src="item.image" alt="">
      <span>{{item.title}}</span>
      <div class="sale">
        <span>{{item.price | money}}</span>
        <span>{{item.saleNum}}人已购买</span>
      </div>
    </div>
    </van-list>
  </div>
</template>

<script>
import { getList } from '@/api/list'
export default {
  name: 'List',
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1
    }
  },
  methods: {
    onLoad () {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      getList(this.$route.query.url, this.page).then(res => {
        console.log(res)
        // 将当前查询出的数据合并到已有的数组中
        this.list = this.list.concat(res)
        // 页面加1
        this.page += 1
        // 标记加载完成
        this.loading = false
        // 当加载新页面数据返回列表长度为0时，说明没有更多数据可以加载了
        if (res.length === 0) {
          this.finished = true
        }
      })
    }
  },
  created () {
    // console.log(this.$route.query.url)
  }

}
</script>

<style lang="scss" scoped>
  .list{
    &-container {
      display:flex;
      flex-wrap: wrap;
      justify-content: space-around;
    }
    &-item {
      width: 46%;
      margin:6px 0;
      color:#666;
      img{
        max-width:100%;
      }
    }
    .sale{
      padding:5px 0;
      font-size:14px;
      display:flex;
      justify-content: space-between;
        span:nth-child(1){
          color:#000;
        }
        span:nth-child(2){
          color:red;
        }
      }
  }
</style>
